import { modelOptions, prop, getModelForClass } from '@typegoose/typegoose'
import { Types } from 'mongoose'
import { ApiProperty } from '@nestjs/swagger'
import { Length } from 'class-validator'

// @plugin(AutoIncrementID, { field: 'roleId', startAt: 1 })
// @index({ roleId: 1 }) // 创建索引,数字代表排序；1=>升序，-1=>降序
@modelOptions({
  schemaOptions: {
    collection: 'roles', //数据库集合的名称
    // 时间填充
    timestamps: true,
  },
})
export class RoleClass {
  /** 角色的名称,方便后台进行权限授权 */
  @prop({ required: true, trim: true, unique: true })
  @Length(2, 20)
  @ApiProperty({
    description: '角色的名称',
    example: '销售*',
    required: true,
  })
  public name: string

  /** 角色的描述 */
  @prop({ required: false, trim: true })
  @ApiProperty({
    description: '角色的描述',
    example: '负责公司销售工作',
    required: false,
  })
  public roleDesc?: string

  /** 角色的权限 */
  @prop({
    type: () => [Types.ObjectId],
    ref: 'PermissionClass',
    required: false,
  })
  @ApiProperty({
    description: '角色的权限',
    example: ['1**'],
    required: false,
  })
  public permissionIds?: Types.ObjectId[]
}

export class bulkQueryDto {
  @ApiProperty({ description: '_id数组', required: true, example: ['2333***'] })
  @prop({ type: () => [Types.ObjectId] })
  public ids: Array<Types.ObjectId>
}

export const RoleModel = getModelForClass(RoleClass)
